<template>
  <van-list
      v-if="currentUser.userRole === 0"
      v-model:loading="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad">
    <van-cell v-for="info in list" :key="info.id">
      订单号:{{ info.id }}
      <br/><br/>
      发货人名称:{{ info.shipperName }}
      <br/><br/>
      收货人名称:{{ info.consigneeName }}
      <br/><br/>
      物流状态:{{ info.entrustStatus }}
      <br/><br/>
      <div className="btn" v-if="info.entrustStatus !== '待取件'">
        <van-button type="primary" round @click="onDetail(info)">查看详情</van-button>
      </div>
      <div className="btn" v-else>
        <van-button type="primary" round @click="onComplete(info)">取件完成</van-button>
      </div>
    </van-cell>
  </van-list>

  <van-list
      v-if="currentUser.userRole === 1 || currentUser.userRole === 2"
      v-model:loading="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad">
    <van-cell v-for="info in list" :key="info.id">
      订单号:{{ info.id }}
      <br/><br/>
      发货人名称:{{ info.shipperName }}
      <br/><br/>
      收货人名称:{{ info.consigneeName }}
      <br/><br/>
      物流状态:{{ info.entrustStatus }}
      <br/><br/>
      <div className="btn" v-if="info.courierId === 0">
        <van-button type="primary" round @click="onAccept(info)">接取订单</van-button>
      </div>
      <div className="btn" v-else-if="info.entrustStatus === '待取件' ">
          <van-button type="primary" round disabled>等待用户取件</van-button>&nbsp;&nbsp;
      </div>
      <div className="btn" v-else>
        <van-button type="primary" round @click="onOver(info)">完成派送</van-button>&nbsp;&nbsp;
        <van-button type="warning" round @click="onClean(info)">取消订单</van-button>
      </div>
    </van-cell>
  </van-list>

</template>

<script setup>
/*
*
id: 1
city: "天津市"
province: "天津市"
county: "河北区"
detailAdd: "各个1号"
postalCode: "120105"
consigneeName: "你好啊"
consigneePhone: "18163282138"
shipperName: "太难了"
shipperPhone: string
* */
import {provide, ref} from "vue";
import {currentUser, GlobalEntrust} from "../config/globalConfig";
import {courierUpdateEntrust, getCurUserEntrustInfos, userComplete} from "../api/request";
import route from "../config/route";
import {Toast} from "vant";


const list = ref([]);
const loading = ref(false);
const finished = ref(false);

const onLoad = async () => {
  let infos = await getCurUserEntrustInfos(currentUser.token)
  infos = infos.data.data
  for (let index in infos) {
    list.value.push(infos[index])
  }
  // 加载状态结束
  loading.value = false;
  // 数据全部加载完成
  finished.value = true;
};

function onDetail(info) {
  Object.assign(GlobalEntrust, info)
  route.push('editEntrust')
}

function updateList(info){
  for (let i=0;i<list.value.length;i++){
    if (list.value[i].id === info.id){
      list.value[i].courierId = info.courierId
      list.value[i].entrustStatus = info.entrustStatus
    }
  }
}

const onAccept = async (info) => {
  /*
  * public class CourierRequestBody {
    String id; // 订单的id
    String token;
    String entrustStatus;
}
  * */
  const requestInfo = {
    id: info.id,
    courierId: currentUser.id,
    token: currentUser.token,
    entrustStatus: '编号[' + currentUser.id + ']昵称[' + currentUser.userName + ']已接受订单',
  }
  try {
    let result = await courierUpdateEntrust(requestInfo)
    result = result.data;
    if (result.statusCode !== 0) {
      Toast.fail(result.message + result.description)
      return
    }
    Toast('订单已接受!')
    updateList(requestInfo)
  } catch (err) {
    Toast.fail(err.message)
  }
}
const onClean = async (info) => {
  const requestInfo = {
    id: info.id,
    courierId: 0,
    token: currentUser.token,
    entrustStatus: '待揽收',
  }
  try {
    let result = await courierUpdateEntrust(requestInfo)
    result = result.data;
    if (result.statusCode !== 0) {
      Toast.fail(result.message + result.description)
      return
    }
    Toast('订单已取消!')
    updateList(requestInfo)
  } catch (err) {
    Toast.fail(err.message)
  }
}
const onOver =async (info) => {
  const requestInfo = {
    id: info.id,
    courierId: currentUser.id,
    token: currentUser.token,
    entrustStatus: '待取件',
  }
  try {
    let result = await courierUpdateEntrust(requestInfo)
    result = result.data;
    if (result.statusCode !== 0) {
      Toast.fail(result.message + result.description)
      return
    }
    Toast('订单已派送!')
    updateList(requestInfo)
  } catch (err) {
    Toast.fail(err.message)
  }
}
const onComplete = async (info) => {
  const requestInfo = {
    id: info.id,
    courierId: currentUser.id,
    token: currentUser.token,
    entrustStatus: '取件完毕',
  }
  try {
    let result = await userComplete(requestInfo)
    result = result.data;
    if (result.statusCode !== 0) {
      Toast.fail(result.message + result.description)
      return
    }
    Toast('订单领取!')
    updateList(requestInfo)
  } catch (err) {
    Toast.fail(err.message)
  }
}
</script>

<style scoped>
.btn {
  float: right;
}
</style>